package phan.connection;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import phan.connection.convert;
import java.io.*;

import java.sql.*;


/**
 * Servlet implementation class for Servlet: LoginServlet
 *
 */
public class OublierLoginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */

    public OublierLoginServlet() {
        super();
    }

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    int i;
    ResultSet src = null;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

System.out.print(request.getParameter("useremail"));
String email="";
if(request.getParameter("useremail")!=null)
            email = request.getParameter("useremail").toString();

            System.out.print("quen mat khau va passwd");
            MySQLDBConnector connect = new MySQLDBConnector();
            src = connect.getOublierLogin(email);
            System.out.print("day la source: "+src);
            boolean check=false;
            while (src.next()) {
                String userLogin = src.getString("UserLogin").toString();
                System.out.print("UserLogin" +userLogin);
                String userPassword = src.getString("UserPassword").toString();
                String UserNom = src.getString("UserNom").toString();
                String UserPrenom = src.getString("UserPrenom").toString();

                convert conv=new convert();
                String host=conv.hostname();
                String from=conv.addmin();
                String to=email;
                phan.connection.ConnuLettre lettre= new ConnuLettre();

                String sujet=lettre.titleOubliez();
                String messeage=lettre.ConnuOubliez(userLogin, userPassword, UserNom, UserPrenom);
                //buitrungdung dong gui mail
           check=  connect.sendMail(host, from, to, sujet, messeage);
            }
            if (check) {
                //request.setAttribute ("user",);
                 request.setAttribute("email", email);
                RequestDispatcher disp;
                disp = request.getRequestDispatcher("/PHAN/OublierLoginSuccess.jsp");
                disp.forward(request, response);
            } else {
                request.setAttribute("isValide", "no");
                RequestDispatcher disp;
                disp = request.getRequestDispatcher("/PHAN/retry.jsp");
                disp.forward(request, response);
            }
        // send ( host, from, to, subject, message );
        } catch (Exception e) {
            System.out.print("loi la 123" + e.getMessage());
        }
    }
}